package com.jprocms.module.cms.controller.portal.api;

import com.jpro.framework.cache.CacheService;
import com.jpro.framework.common.pojo.CommonResult;
import com.jpro.framework.common.pojo.PageResult;
import com.jpro.framework.tenant.core.db.dynamic.TenantDS;
import com.jpro.module.system.api.dict.DictDataApi;
import com.jpro.module.system.api.dict.dto.DictDataRespDTO;
import com.jprocms.module.cms.cache.MyRedisHelper;
import com.jprocms.module.cms.controller.admin.link.vo.LinkListReqVO;
import com.jprocms.module.cms.controller.admin.link.vo.LinkPageReqVO;
import com.jprocms.module.cms.controller.admin.link.vo.LinkRespVO;
import com.jprocms.module.cms.convert.link.LinkConvert;
import com.jprocms.module.cms.dal.dataobject.link.LinkDO;
import com.jprocms.module.cms.service.link.LinkService;
import com.jprocms.module.cms.service.site.SiteService;
import com.jprocms.module.cms.util.CmsContextUtil;
import com.jprocms.module.cms.web.Directives;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static com.jpro.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.jpro.framework.common.pojo.CommonResult.success;
import static com.jprocms.module.cms.constant.CmsSysConstants.CMS_LIST_MAX_COUNT;
import static com.jprocms.module.cms.constant.CmsSysConstants.FRONT_API;
import static com.jprocms.module.cms.dal.redis.RedisKeyConstants.DICT_DATA;
import static com.jprocms.module.cms.enums.ErrorCodeConstants.*;

/**
 * 数据字典 Controller
 *
 * @author jprocms
 */
@TenantDS
@Tag(name = "数据字典接口", description = "数据字典接口")
@RestController
@RequestMapping({FRONT_API + "/dict/data"})
public class DictApiController {
    private MyRedisHelper cacheService;

    private final DictDataApi dictDataApi;

    public DictApiController(MyRedisHelper cacheService, DictDataApi dictDataApi) {
        this.cacheService = cacheService;
        this.dictDataApi = dictDataApi;
    }

    @Operation(summary = "获取数据字典列表（同ZDictList标签）")
    @GetMapping
    public CommonResult<List<DictDataRespDTO>> list(String type) {
        if(StringUtils.isBlank(type)){
            throw exception(COMMON_PARAM_ERROR);
        }
        final Object typeAliasCache = cacheService.hget(DICT_DATA, type);
        List<DictDataRespDTO> list;
        if (typeAliasCache != null && typeAliasCache instanceof List) {
            list = (List<DictDataRespDTO>) typeAliasCache;
        } else {
            CommonResult<List<DictDataRespDTO>> dictDataByTypeResp = dictDataApi.getDictDataByType(type);
            list = Collections.emptyList();
            if (!dictDataByTypeResp.isError()) {
                list = dictDataByTypeResp.getData();
                //缓存10分钟
                cacheService.hset(DICT_DATA, type, list,600);
            }
        }
        return success(list);
    }

}
